package com.yingdong.tools;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateNotFoundException;

public class ExcelTemplateGenerator {

	public static void main(String[] args) {

		// 开始设置Freemarker
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("UTF-8");
		// 设置模板文件所在的目录
		cfg.setClassForTemplateLoading(ExcelTemplateGenerator.class, "/template");
		Writer out = null;
		try {
			// 取得模板文件
			Template t = cfg.getTemplate("ExcelTemplate.ftl");
			File[] files = new File("../ydserver/excel").listFiles();
			for (File file : files) {
				if (file.isFile() && !file.isHidden() && file.getName().contains(".xlsx")) {
					String fileName = file.getName().split("\\.")[0];
					Map root = new HashMap();
					List fields = Excel2007Reader.readFieldsFromFile("../ydserver/excel/" + fileName + ".xlsx");
					String comment = Excel2007Reader.readClassComment("../ydserver/excel/" + fileName + ".xlsx");
					root.put("fields", fields);
					root.put("classname", fileName);
					root.put("classcomment", comment);
					out = new FileWriter("src/main/java/com/yingdong/excel/" + fileName + ".java");
					t.process(root, out);
					out.close();
				}
			}

		} catch (Exception e1) {

			e1.printStackTrace();
		} finally {
			try {

			} catch (Exception e) {

				e.printStackTrace();
			}
		}

	}

	public static void generateExcelTemplate(IFile file) throws Exception {
		if(!file.getFileExtension().equals("xlsx")){
			throw new Exception("文件不是标准的Excel类型");
		}
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("UTF-8");
		// 设置模板文件所在的目录
		cfg.setClassForTemplateLoading(ExcelTemplateGenerator.class, "/template");
		Writer out = null;
		File f = file.getLocation().makeAbsolute().toFile();
		String path = f.getAbsolutePath();
		String fileName = file.getName().split("\\.")[0];
		// 取得模板文件
		Template t = cfg.getTemplate("ExcelTemplate.ftl");
		Map root = new HashMap();
		List fields = Excel2007Reader.readFieldsFromFile(path);
		String comment = Excel2007Reader.readClassComment(path);
		root.put("fields", fields);
		root.put("classname", fileName);
		root.put("classcomment", comment);
		out = new StringWriter();
		t.process(root, out);
		;
		IProject project = file.getProject();
		IFile writeFile = project.getFile("src/com/yingdong/excel/" + fileName + ".java");

		InputStream source = new ByteArrayInputStream(out.toString().getBytes("utf8"));
		if (writeFile.exists()) {
			writeFile.delete(true, null);
		}
		writeFile.create(source, IResource.FILE, null);
		source.close();
		out.close();

	}
}
